package com.xjm.bean.definition;

import com.xjm.model.Child;
import com.xjm.model.Root;
import org.springframework.beans.factory.support.ChildBeanDefinition;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

/**
 * @author jaymin<br>
 * <p>
 * 2020/11/29 14:20
 */
@SuppressWarnings("all")
public class ChildBeanDefinitionDemo {

	public static void main(String[] args) {
		// 1. 构建一个空的容器
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
		String parentName = "rootB";
		String childName = "child";
		RootBeanDefinition rootBeanDefinition = RootBeanDefinitionDemo.getRootBeanDefinitionBySetter();
		ChildBeanDefinition childBeanDefinition = new ChildBeanDefinition(parentName);
		childBeanDefinition.setBeanClass(Child.class);
		childBeanDefinition.getPropertyValues().addPropertyValue("parentName", parentName);
		childBeanDefinition.getPropertyValues().addPropertyValue("isRoot", false);
		// 注册
		applicationContext.registerBeanDefinition(parentName, rootBeanDefinition);
		applicationContext.registerBeanDefinition(childName, childBeanDefinition);
		// 刷新上下文
		applicationContext.refresh();
		Root root = applicationContext.getBean(Root.class);
		Child child = (Child) applicationContext.getBean(childName);
		System.out.println(root.toString());
		System.out.println(child.toString());
	}
}
